package com.dxy.dxythird.controller;

import com.dxy.dxythird.service.QiniuUploadFileService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * junfeng 2018-11-01 13:30:21 七牛云图片上传控制器
 */
@RestController
@RequestMapping("/qiniu")
public class QiniuUploadController {

    @Autowired
    private QiniuUploadFileService uploadFileService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 上传七牛云接口
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile) {
        FileInputStream inputStream = null;
        try {
            inputStream = (FileInputStream) multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = uploadFileService.uploadFileToQiniu(inputStream);
        // 消息队列
        amqpTemplate.convertAndSend("imageUrlSave", url);
        return url;
    }


}
